<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form
            inline
            size="small"
            :model="from"
            ref="from"
            :label-position="labelPosition"
            label-width="100px"
          >
            <el-row>
              <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                <el-form-item label="品种名称：">
                  <el-select
                    v-model="from.exchangeCommodityId"
                    placeholder="请选择品种名称"
                    class="selWidth"
                    clearable
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.id"
                      :label="item.commodityName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="状态：">
                  <el-select
                    v-model="from.status"
                    placeholder="请选择"
                    class="selWidth"
                    clearable
                  >
                    <el-option :value="1" label="正常"></el-option>
                    <el-option :value="2" label="停用"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col
                :xs="24"
                :sm="24"
                :md="24"
                :lg="6"
                :xl="6"
                class="text-right from"
              >
                <el-form-item>
                  <el-button
                    class="mr10"
                    size="small"
                    type="primary"
                    icon="el-icon-search" 
                    @click="search()"
                    >搜索</el-button
                  >
                  <el-button class="mr10" icon="el-icon-refresh" size="small" @click="reset()"
                    >重置</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="btn_bt">
          <el-button class="mr10" icon="el-icon-circle-plus-outline"  size="small" type="primary" @click="add()"
            >新增</el-button
          >
          <el-button class="mr10" icon="el-icon-delete"  size="small" type="danger" @click="allDel()"
            >删除</el-button
          >
          <el-button class="mr10" icon="el-icon-top"  size="small" @click="exportTable()"
            >导出表格</el-button
          >
        </div>
      </div>
      <el-table border
        ref="table"
        v-loading="loading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        @selection-change="onSelectTab"
        highlight-current-row
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="commodityName"
          label="品种名称"
          min-width="100"
        />
        <el-table-column
          prop="tradeTimeVoList"
          label="品种强平配置"
          min-width="350"
        >
          <template slot-scope="scope">
            <div
              v-for="(item, index) in scope.row.tradeTimeVoList"
              :key="index"
            >
              <span
                >交易时间段：{{ item.tradeTime.beginTime }}-{{
                  item.tradeTime.endTime
                }}</span
              >
              <span v-if="item.status == 1" style="margin-left: 10px"
                >强平时间：{{ item.marginCloseoutTime }}</span
              >
              <i
                v-if="item.status == 1"
                class="el-icon-check"
                style="color: #1890ff; font-size: 16px; font-weight: bold"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="80">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" v-if="scope.row.status == 1"
              >正常</el-button
            >
            <el-button type="info" size="mini" v-if="scope.row.status == 2"
              >停用</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="100" />
        <el-table-column prop="createTime" label="创建时间" min-width="150" />
        <el-table-column
          label="操作"
          min-width="130"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[15, 30, 45, 60]"
          :page-size="from.limit"
          :current-page="from.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <el-dialog
      :title="ruleForm.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="800px"

      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :rules="rules"
        ref="ruleForm"
        :model="ruleForm"
        label-width="150px"
      >
        <el-form-item label="品种名称：" prop="exchangeCommodityId">
          <el-select
            v-model="ruleForm.exchangeCommodityId"
            :disabled="ruleForm.id ? true : false"
            placeholder="请选择品种名称"
            @change="handleChange"
            class="selWidth"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.commodityName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="品种强平配置："
          v-if="ruleForm.exchangeCommodityId"
        >
          <el-table border
            :data="ruleForm.tradeTimeVoList"
            height="250"
            border
            style="width: 100%"
          >
            <el-table-column label="交易时间段">
              <template slot-scope="scope">
                <span
                  >{{ scope.row.tradeTime.beginTime }}-{{
                    scope.row.tradeTime.endTime
                  }}</span
                >
              </template>
            </el-table-column>
            <el-table-column label="品种强平时间">
              <template slot-scope="scope">
                <el-time-picker
                  style="width: 100%"
                  v-model="scope.row.marginCloseoutTime"
                  :value-format="'HH:mm:ss'"
                  :picker-options="{
                    selectableRange: '00:00:00 - 23:59:59',
                  }"
                  placeholder="请选择强平时间"
                >
                </el-time-picker>
              </template>
            </el-table-column>
            <el-table-column label="是否启用">
              <template slot-scope="scope">
                <el-radio-group v-model="scope.row.status">
                  <el-radio :label="1">启用</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="状态：">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input type="textarea" v-model="ruleForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  exchangecommodityGetOption,
  exchangecommodityGetRiskOption,
} from "@/api/transaction";
import {
  commodityriskList,
  commodityriskSave,
  commodityriskInfo,
  commodityriskUpdate,
  commodityriskDelete,
  commodityriskExport,
} from "@/api/agent";
export default {
  name: "commodityrisk",
  data() {
    return {
      labelPosition: "right",
      options: [],
      from: {
        exchangeCommodityId: "",
        status: 1,
        page: 1,
        limit: 15,
      },
      loading: false,
      tableData: {
        data: [],
        total: 0,
      },
      selection: [],
      dialogVisible: false,
      ruleForm: {
        id: "",
        exchangeCommodityId: "",
        tradeTimeVoList: [],
        status: 1,
        remark: "",
      },
      rules: {
        exchangeCommodityId: [
          {
            required: true,
            message: "请选择品种名称",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    exchangecommodityGetOption()
      .then((res) => {
        console.log("品种全列表", res);
        this.options = res;
        this.getlist();
      })
      .catch((err) => {
        console.log(err);
      });
  },
  methods: {
    search() {
      console.log(this.from);
      this.from.page = 1;
      this.from.limit = 15;
      this.getlist();
    },
    reset() {
      this.from = {
        exchangeCommodityId: "",
        status: 1,
        page: 1,
        limit: 15,
      };
      this.getlist();
    },
    getlist() {
      this.loading = true;
      commodityriskList(this.from)
        .then((res) => {
          console.log(res);
          this.tableData.data = res.list;
          this.tableData.data.forEach((item) => {
            item.tradeTimeVoList.forEach((items) => {
              items.tradeTime = JSON.parse(items.tradeTime);
            });
          });
          this.tableData.total = res.total;
          this.loading = false;
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
    },
    add() {
      this.ruleForm = {
        id: "",
        exchangeCommodityId: "",
        tradeTimeVoList: [],
        status: 1,
        remark: "",
      };
      this.dialogVisible = true;
    },
    handleChange(id) {
      console.log("品种id", id);
      exchangecommodityGetRiskOption(id)
        .then((res) => {
          console.log("品种强平列表", res);
          this.ruleForm.tradeTimeVoList = res;
          this.ruleForm.tradeTimeVoList.forEach((item) => {
            item.tradeTime = JSON.parse(item.tradeTime);
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    close() {
      this.ruleForm = {
        id: "",
        exchangeCommodityId: "",
        tradeTimeVoList: [],
        status: 1,
        remark: "",
      };
      this.dialogVisible = false;
    },
    submit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            console.log("编辑", this.ruleForm);
            let data = {
              id: this.ruleForm.id,
              exchangeCommodityId: this.ruleForm.exchangeCommodityId,
              tradeTimeVoList: this.ruleForm.tradeTimeVoList,
              status: this.ruleForm.status,
              remark: this.ruleForm.remark,
            };
            data.tradeTimeVoList.forEach((item) => {
              item.tradeTime = JSON.stringify(item.tradeTime);
            });
            commodityriskUpdate(data)
              .then((res) => {
                console.log(res);
                this.close();
                this.$message.success("编辑成功");
                this.getlist();
              })
              .catch((err) => {
                console.log(err);
                this.$message.error(err);
              });
          } else {
            console.log("新增", this.ruleForm);
            let data = {
              exchangeCommodityId: this.ruleForm.exchangeCommodityId,
              tradeTimeVoList: this.ruleForm.tradeTimeVoList,
              status: this.ruleForm.status,
              remark: this.ruleForm.remark,
            };
            console.log(data.tradeTimeVoList);
            data.tradeTimeVoList.forEach((item) => {
              item.tradeTime = JSON.stringify(item.tradeTime);
            });
            commodityriskSave(data)
              .then((res) => {
                console.log(res);
                this.close();
                this.$message.success("新增成功");
                this.getlist();
              })
              .catch((err) => {
                this.ruleForm.tradeTimeVoList.forEach((item) => {
                  item.tradeTime = JSON.parse(item.tradeTime);
                });
                this.$message.error(err);
              });
          }
        } else {
          this.$message.error("请填写完整信息");
        }
      });
    },
    // 选择
    onSelectTab(selection) {
      this.selection = [];
      selection.forEach((item) => {
        this.selection.push(item.id);
      });
      console.log(this.selection);
    },
    allDel() {
      if (this.selection.length == 0) {
        return this.$message.error("至少选择一项");
      }
      console.log(this.selection);
      this.$confirm("确认删除？")
        .then((_) => {
          commodityriskDelete(this.selection)
            .then((res) => {
              console.log(res);
              this.$message.success("删除成功");
              this.getlist();
            })
            .catch((err) => {
              console.log(err);
              this.$message.error(err);
            });
        })
        .catch((_) => {});
    },
    exportTable() {
      commodityriskExport(this.from)
        .then((res) => {
          console.log("导出表格", res);
          window.open(res.filename);
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err);
        });
    },
    handleEdit(row) {
      console.log("编辑", row);
      commodityriskInfo(row.id)
        .then((res) => {
          console.log("详情", res);
          this.ruleForm = {
            id: res.id,
            exchangeCommodityId: res.exchangeCommodityId,
            tradeTimeVoList: res.tradeTimeVoList,
            status: Number(res.status),
            remark: res.remark,
          };
          this.ruleForm.tradeTimeVoList.forEach((item) => {
            item.tradeTime = JSON.parse(item.tradeTime);
          });
          this.dialogVisible = true;
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err);
        });
    },
    handleDel(row) {
      this.$confirm("确认删除？")
        .then((_) => {
          console.log("删除", row);
          commodityriskDelete([row.id])
            .then((res) => {
              console.log(res);
              this.$message.success("删除成功");
              this.getlist();
            })
            .catch((err) => {
              console.log(err);
              this.$message.error(err);
            });
        })
        .catch((_) => {});
    },
    pageChange(page) {
      this.from.page = page;
      this.getlist();
    },
    handleSizeChange(val) {
      this.from.limit = val;
      this.getlist();
    },
  },
};
</script>

<style scoped lang="scss">
.btn_bt {
  border-top: 1px dashed #ccc;
  padding-top: 20px;
}
</style>
